// BezierEditor.cpp : implementation file
//

#include "stdafx.h"
#include "CGWork.h"
#include "BezierEditor.h"


// CBezierEditor dialog

IMPLEMENT_DYNCREATE(CBezierEditor, CDialog)

CBezierEditor::CBezierEditor(CWnd* pParent /*=NULL*/,int degree)
	: CDialog(CBezierEditor::IDD, pParent)
	, m_x(0)
	, m_y(0)
	, m_z(0)
	,m_degree(degree)
	, m_ctlPtId(_T(""))
{
	
}

CBezierEditor::~CBezierEditor()
{
}

void CBezierEditor::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_X, m_x);
	DDX_Text(pDX, IDC_Y, m_y);
	DDX_Text(pDX, IDC_Z, m_z);
	DDX_Control(pDX, IDC_CTLPT_ID, m_ctlPtIdSelctor);
	DDX_CBString(pDX, IDC_CTLPT_ID, m_ctlPtId);
}

BOOL CBezierEditor::OnInitDialog()
{
	CDialog::OnInitDialog();
	CString str;
	for (int i =0;i<m_degree;i++){
		str.Format("%d",i);
		m_ctlPtIdSelctor.AddString(str);
	}
	m_ctlPtIdSelctor.SelectString(-1,m_ctlPtId);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

BEGIN_MESSAGE_MAP(CBezierEditor, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
	ON_BN_CLICKED(IDC_EDIT_POINT, OnBnClickedEditPoint)
	ON_CBN_SELCHANGE(IDC_CTLPT_ID, OnCbnSelchangeCtlptId)
	ON_CBN_EDITCHANGE(IDC_CTLPT_ID, OnCbnEditchangeCtlptId)
END_MESSAGE_MAP()


void CBezierEditor::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CBezierEditor::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

void CBezierEditor::OnBnClickedEditPoint()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	int id = atoi(LPCTSTR(m_ctlPtId));
	m_ctlPt[id].m_x = m_x;
	m_ctlPt[id].m_y = m_y;
	m_ctlPt[id].m_z = m_z;
	
}

void CBezierEditor::OnCbnSelchangeCtlptId()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	int id = atoi(LPCTSTR(m_ctlPtId));
	m_x = m_ctlPt[id].m_x;
	m_y = m_ctlPt[id].m_y;
	m_z = m_ctlPt[id].m_z;
	UpdateData(FALSE);
	
}

void CBezierEditor::OnCbnEditchangeCtlptId()
{
	// TODO: Add your control notification handler code here
}

void CBezierEditor::SetCtlPoint(int ptid, double x, double y, double z)
{
	if(ptid < 0 && ptid >= m_degree){
		AfxMessageBox("Not a valid PtId");
		return;
	}
	m_ctlPt[ptid].m_x = x;
	m_ctlPt[ptid].m_y = y;
	m_ctlPt[ptid].m_z = z;
}

bool CBezierEditor::GetCtlPoint(int ptid, double &x, double &y, double &z)
{
	if(ptid < 0 && ptid >= m_degree)
		return false;
	x = m_ctlPt[ptid].m_x;
	y = m_ctlPt[ptid].m_y;
	z = m_ctlPt[ptid].m_z;
	
	return true;
}
